﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace Nina.Examples
{
    /// <summary>
    /// Protocol handler for client
    /// </summary>
    class ClientProtocolHandler : NinaProtocolHandler
    {

        /// <summary>
        /// Called when a message is received from the server
        /// </summary>
        /// <param name="s">NinaSession from which the message was received</param>
        /// <param name="data">The data received</param>
        public override void MessageReceived(NinaSession s, byte[] data)
        {
            /* Print out received message */
            Console.WriteLine("Client received a message: " + Encoding.UTF8.GetString(data));
        }

        /// <summary>
        /// Called when the session is closed
        /// </summary>
        /// <param name="s">The session which was closed</param>
        public override void SessionClosed(NinaSession s)
        {
        }

        /// <summary>
        /// Called when the session is opened
        /// </summary>
        /// <param name="s">The session which was opened</param>
        public override void SessionOpened(NinaSession s)
        {
            /* Say hello to the server */
            for (int i = 1; i <= 1000; i++)
            {
                byte[] data = Encoding.UTF8.GetBytes("Hello server (Message " + i.ToString() + ")");
                s.Write(data);
            }
        }
    }
}
